import * as PIXI from 'pixi.js'

// 原本的水滴效果shader
export default class SuperFilter {
  myShaderVert = `
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;

uniform mat3 projectionMatrix;
uniform mat3 otherMatrix;

varying vec2 vMaskCoord;
varying vec2 vTextureCoord;

void main(void)
{
    gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);

    vTextureCoord = aTextureCoord;
    vMaskCoord = ( otherMatrix * vec3( aTextureCoord, 1.0)  ).xy;
}
  `

  myShaderFrag = `
varying vec2 vMaskCoord;
varying vec2 vTextureCoord;

uniform sampler2D uSampler;
uniform sampler2D mask;

void main(void)
{
  vec4 masky =  texture2D(mask, vMaskCoord);
  float strength =  (masky.r * masky.a);
  strength *= 5.0;
  strength = min(1.0, strength ) ;
  vec4 original =  texture2D(uSampler, vTextureCoord +  (1.0-strength) * 0.1);
  original *= strength;
  gl_FragColor =  original;
}

`
  constructor(mask) {
    let e = new PIXI.Matrix()
    // console.log(mask.texture)
    return new PIXI.Filter(this.myShaderVert, this.myShaderFrag, {
      mask: { type: 'sampler2D', value: mask.texture },
      otherMatrix: { type: 'mat3', value: e.toArray(true) },
    })
  }
}
